package com.ruoyi.product.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Objects;
import java.util.stream.Collectors;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.constant.CommonConst;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.IdGenerator;
import com.ruoyi.product.dto.ClassifyDTO;
import com.ruoyi.product.utils.TreeUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.ProductClassifyMapper;
import com.ruoyi.product.domain.ProductClassify;
import com.ruoyi.product.service.IProductClassifyService;
import com.ruoyi.common.core.text.Convert;

/**
 * 产品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-27
 */
@Service
public class ProductClassifyServiceImpl implements IProductClassifyService {

    @Autowired
    private ProductClassifyMapper productClassifyMapper;

    /**
     * 查询产品分类
     *
     * @param keyId 产品分类ID
     * @return 产品分类
     */
    @Override
    public ProductClassify selectProductClassifyById(Long keyId) {
        return productClassifyMapper.selectProductClassifyById(keyId);
    }

    /**
     * 查询产品分类列表
     *
     * @param productClassify 产品分类
     * @return 产品分类
     */
    @Override
    public List<ProductClassify> selectProductClassifyList(ProductClassify productClassify) {
        return productClassifyMapper.selectProductClassifyList(productClassify);
    }

    /**
     * 新增产品分类
     *
     * @param productClassify 产品分类
     * @return 结果
     */
    @Override
    public int insertProductClassify(ProductClassify productClassify) {
        productClassify.setCreateTime(DateUtils.getNowDate());
        productClassify.setKeyId(IdGenerator.getId());
        if (productClassify.getOrderNum() == null) {
            productClassify.setOrderNum(1);
        }
        return productClassifyMapper.insertProductClassify(productClassify);
    }

    /**
     * 修改产品分类
     *
     * @param productClassify 产品分类
     * @return 结果
     */
    @Override
    public int updateProductClassify(ProductClassify productClassify) {
        productClassify.setUpdateTime(DateUtils.getNowDate());
        return productClassifyMapper.updateProductClassify(productClassify);
    }

    /**
     * 删除产品分类对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteProductClassifyByIds(String ids) {
        String[] idsStr = Convert.toStrArray(ids);
        return Arrays.stream(idsStr).distinct().mapToInt(id -> deleteProductClassifyById(Long.valueOf(id))).sum();
    }

    /**
     * 删除产品分类信息
     *
     * @param keyId 产品分类ID
     * @return 结果
     */
    @Override
    public int deleteProductClassifyById(Long keyId) {
        List<ClassifyDTO> classifyDTOS = coverClassify2DTO();

        List<Long> classifyIds = CollUtil.newArrayList(keyId);
        String[] childIds = TreeUtils.getChildIds(classifyIds, classifyDTOS, keyId).stream().distinct().map(String::valueOf).toArray(String[]::new);
        return productClassifyMapper.deleteProductClassifyByIds(childIds);
    }

    /**
     * 查询产品分类树列表
     *
     * @return 所有产品分类信息
     */
    @Override
    public List<Ztree> selectProductClassifyTree() {
        List<ProductClassify> productClassifyList = productClassifyMapper.selectProductClassifyList(new ProductClassify());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (ProductClassify productClassify : productClassifyList) {
            Ztree ztree = new Ztree();
            ztree.setId(productClassify.getKeyId());
            ztree.setpId(productClassify.getParentId());
            ztree.setName(productClassify.getName());
            ztree.setTitle(productClassify.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    /**
     * 将数据库中的分类对象转为DTO对象
     * @return
     */
    public List<ClassifyDTO> coverClassify2DTO () {
        ProductClassify query = new ProductClassify();
        query.setDelStatus(CommonConst.DEL_FALSE);
        List<ProductClassify> productClassifies = productClassifyMapper.selectProductClassifyList(query);
        if (CollUtil.isEmpty(productClassifies)) {
            return Lists.newArrayList();
        }
        List<ClassifyDTO> classifyDTOs = productClassifies.stream().map(p -> {
            ClassifyDTO dto = new ClassifyDTO();
            BeanUtils.copyProperties(p, dto);
            return dto;
        }).filter(Objects::nonNull).collect(Collectors.toList());
        return classifyDTOs;
    }

    @Override
    public AjaxResult getTreeList() {
        List<ClassifyDTO> classifyDTOs = coverClassify2DTO();
        if (CollUtil.isEmpty(classifyDTOs)) {
            return AjaxResult.success();
        }
        List<ClassifyDTO> treeList = TreeUtils.getTreeList(0L, classifyDTOs);
        return AjaxResult.success(treeList);
    }

}
